<!DOCTYPE html>
<html>
	<head>
		<title>轮播图-添加/修改</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<!-- 所有的 css js 资源 -->
		<link rel="stylesheet" href="https://unpkg.com/element-ui@2.13.0/lib/theme-chalk/index.css">
		<link rel="stylesheet" href="../../static/sa.css"> 
		<script src="https://unpkg.com/vue@2.6.10/dist/vue.js"></script>
		<script src="https://unpkg.com/element-ui@2.13.0/lib/index.js"></script>
		<script src="https://unpkg.com/http-vue-loader@1.4.2/src/httpVueLoader.js"></script>
		<script src="https://unpkg.com/jquery@3.4.1/dist/jquery.js"></script>
		<script src="https://www.layuicdn.com/layer-v3.1.1/layer.js"></script>
		<script src="../../static/sa.js"></script>
		<script src="../../static/kj/upload-util.js"></script>
		<style type="text/css">
			.td-img{width: 400px; height: 200px; cursor: pointer; vertical-align: middle;}
			.c-panel .el-form .c-label{width: 5em !important;}
			.c-panel .el-form .el-input{width: 400px;}
		</style>
	</head>
	<body>
		<div class="vue-box" :class="{sbot: id}" style="display: none;" :style="'display: block;'">
			<!-- ------- 内容部分 ------- -->
			<div class="s-body">
				<div class="c-panel">
					<div class="c-title">{{ id <= 0 ? '新增轮播图' : '修改轮播图' }}</div>
					<el-form v-if="m">
						<sa-item name="图片" br>
							<img :src="getImageUrl(m.image)" class="td-img" @click="sa.showImage(getImageUrl(m.image), '70%', '80%')" v-if="m.image">
							<div v-else style="width: 400px; height: 200px; border: 2px dashed #d9d9d9; display: flex; align-items: center; justify-content: center; color: #999;">
								请上传图片
							</div>
							<el-link type="primary" @click="uploadImage" style="margin-top: 10px; display: block;">上传图片</el-link>
						</sa-item>
						<sa-item name="标题" v-model="m.title" br></sa-item>
						<sa-item name="" class="s-ok" br>
							<el-button type="primary" icon="el-icon-plus" @click="ok()">保存</el-button>
						</sa-item>
					</el-form>
				</div>
			</div>
			<!-- ------- 底部按钮 ------- -->
			<div class="s-foot">
				<el-button type="primary" @click="ok()">确定</el-button>
				<el-button @click="sa.closeCurrIframe()">取消</el-button>
			</div>
		</div>
		<script src="./mock-data-list.js"></script>
        <script>
			
			var app = new Vue({
				components: {
					"sa-item": httpVueLoader('../../sa-frame/com/sa-item.vue'),		
					"sa-info": httpVueLoader('../../sa-frame/com/sa-info.vue'),		
				},
				el: '.vue-box',
				data: {
					id: sa.p('id', 0),		// 获取超链接中的id参数（0=添加，非0=修改） 
					m: null,		// 实体对象 
				},
				methods: {
					// 获取图片完整URL
					getImageUrl: function(imageName) {
						if (!imageName) return '';
						// 如果已经是完整URL，直接返回
						if (imageName.startsWith('http')) {
							return imageName;
						}
						// 否则拼接基础路径
						return 'http://localhost:8080/images/' + imageName;
					},
					// 上传图片
					uploadImage: function() {
						sa.uploadImage(src => {
							// 从完整URL中提取文件名
							let fileName = src.split('/').pop();
							this.m.image = fileName;
							sa.ok2('上传成功');
						});
					},
					// 创建一个 默认Model 
					createModel: function() {
						return {
							id: null, 
							title: '',
							image: '',
							createTime: null
						}
					},
					// 提交数据 
					ok: function(){
						// 验证 
						sa.checkNull(this.m.title, '请输入标题');
						sa.checkNull(this.m.image, '请上传图片');
						
						// 准备提交的数据
						let submitData = {
							title: this.m.title,
							image: this.m.image
						};
						
						if(this.id > 0) {
							submitData.id = this.id;
						}
						
						// 开始增加或修改
						if(this.id <= 0) {	// 添加
							$.ajax({
								url: 'http://localhost:8080/banner/addBanner',
								type: 'GET',
								data: submitData,
								success: function(res) {
									if (res.code === 200) {
										sa.alert('增加成功', function() {
											if(parent.app) {
												parent.app.f5();		// 刷新父页面列表
												sa.closeCurrIframe();	// 关闭本页 
											} else {
												app.m = this.createModel();
											}
										}.bind(this)); 
									} else {
										sa.msg('添加失败：' + res.message);
									}
								}.bind(this),
								error: function(xhr, status, error) {
									sa.msg('添加失败：' + error);
								}
							});
						} else {	// 修改
							$.ajax({
								url: 'http://localhost:8080/banner/updateBanner',
								type: 'GET',
								data: submitData,
								success: function(res) {
									if (res.code === 200) {
										sa.alert('修改成功', this.clean);
									} else {
										sa.msg('修改失败：' + res.message);
									}
								}.bind(this),
								error: function(xhr, status, error) {
									sa.msg('修改失败：' + error);
								}
							});
						}
					},
					// 添加/修改 完成后的动作
					clean: function() {
						if(this.id == 0) {
							this.m = this.createModel();
						} else {
							parent.app.f5();		// 刷新父页面列表
							sa.closeCurrIframe();	// 关闭本页 
						}
					}
				},
				mounted: function(){
					// 初始化数据 
					if(this.id <= 0) {	
						this.m = this.createModel();
					} else {	
						// 从父页面获取数据或使用模拟数据
						if(parent.app && parent.app.dataList) {
							let item = parent.app.dataList.find(item => item.id == this.id);
							if(item) {
								this.m = {
									id: item.id,
									title: item.title,
									image: item.image,
									createTime: item.createTime
								};
							} else {
								this.m = this.createModel();
							}
						} else {
							this.m = this.createModel();
						}
					}
				}
			})
		</script>
	</body>
</html>